<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <h1>动态组件</h1>
    <!-- <button @click="show = 1">com1</button>
    <button @click="show = 2">com2</button>
    <button @click="show = 3">com3</button> -->

    <!-- <child1 v-if="show === 1"></child1>
    <child2 v-if="show === 2"></child2>
    <child3 v-if="show === 3"></child3> -->


    <button @click="str = 'child1'">com1</button>
    <button @click="str = 'child2'">com2</button>
    <button @click="str = 'child3'">com3</button>
    <!-- 动态组件 -->
    <!-- <component :is="str"></component> -->
    <!-- 里面的组件在切换的时候，会销毁和重新创建 -->
    <!-- keep-alive可以让动态组件里面切换的组件进行缓存(切换的时候不会销毁和重新创建) -->
    <keep-alive>
      <component :is="str"></component>
    </keep-alive>
  </div>

  <script src="../Vue.js"></script>

  <script>
    const { createApp } = Vue;

    const Child1 = {
      template: `
        <div>这是child1组件</div>
      `
    }

    const Child2 = {
      data() {
        return {
          count: 1
        }
      },
      template: `
        <div>这是child2组件 - {{ count }}</div>
      `,
      mounted() {
        console.log("mounted");
        
        setInterval(() => {
          this.count++
        }, 1000)
      },
      beforeUnmount() {
        console.log("beforeUnmount");
        
      }
    }

    const Child3 = {
      template: `
        <div>这是child3组件</div>
      `
    }

    createApp({
      data() {
        return {
          // show: 1
          str: "child1"
        }
      },
      methods: {},
      components: {
        Child1,
        Child2,
        Child3,

      }
    }).mount("#app")
  </script>
</body>
</html>